package window;

import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AllWindowedStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

import java.text.SimpleDateFormat;

/**
 * 使用event time划分滚动窗口
 * non-key window和window function并行度都为1
 */
public class EventTimeTumblingWindowAllDemo3 {

    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());

        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);

        // flink 的时间精确到 毫秒级别
        // Watermarks (水位线，窗口触发机制)
        // 当前窗口的水位线 = 窗口中最大的eventTime - 延迟时间
        // 水位线 >= (窗口结束时间 - 1ms) 就会触发计算
        // BoundedOutOfOrdernessTimestampExtractor 有界的 允许数据乱序
        SingleOutputStreamOperator<String> watermarkDataStream = lines.assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<String>(Time.seconds(0)) {
            @Override
            public long extractTimestamp(String s) {
                return Long.parseLong(s.split(",")[0]);
            }
        });

        // 处理数据
        SingleOutputStreamOperator<Tuple2<String,Integer>> mapped = watermarkDataStream.map(new MapFunction<String, Tuple2<String,Integer>>() {
            @Override
            public Tuple2<String,Integer> map(String s) throws Exception {
                String[] fields = s.split(",");
                return Tuple2.of(fields[1],Integer.parseInt(fields[2]));
            }
        });

        // 根据EventTime划分窗口
        AllWindowedStream<Tuple2<String,Integer>, TimeWindow> window = mapped.windowAll(TumblingEventTimeWindows.of(Time.seconds(5)));

        // 聚合
        SingleOutputStreamOperator<Tuple2<String, Integer>> summed = window.aggregate(new AggregateFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, Tuple2<String, Integer>>() {

            // 定义累加器
            @Override
            public Tuple2<String, Integer> createAccumulator() {
                return Tuple2.of(null,0);
            }

            // 累加
            @Override
            public Tuple2<String, Integer> add(Tuple2<String, Integer> value, Tuple2<String, Integer> acc) {
                value.f1 = value.f1 + acc.f1;
                return value;
            }

            // 返回结果
            @Override
            public Tuple2<String, Integer> getResult(Tuple2<String, Integer> acc) {
                return acc;
            }

            /**
             * merge 方法只有是SessionWindow的时候才有可能调用
             * @param stringIntegerTuple2
             * @param acc1
             * @return
             */
            @Override
            public Tuple2<String, Integer> merge(Tuple2<String, Integer> stringIntegerTuple2, Tuple2<String, Integer> acc1) {
                return null;
            }
        });

        // 打印
        summed.print();
        env.execute();
    }
}
